const express = require('express')
let modelsessions = require('../model/sessions')
let router = express.Router()
let usermodel = require('../model/user')
let mongodb = require('../../public/mongo/mongodb')
const session = require('express-session')
router.post('/reg', (req, res) => {
  if (req.session.username) {
    console.log('也存在');

  } else {
    console.log('不存在');
  }
})
router.post('/login', (req, res) => {
  let { username, } = req.body
  mongodb(() => {
    console.log('链接成功');
    modelsessions.find()
      .then((result) => {
        if (result[0]) {
          return res.send('已登录')
        } else {
          // 判断是否已注册
          usermodel.find({ name: username })
            .then(result => {
              if (result[0]) {
                req.session.username = result[0].name
                return res.send('登录成功')
              } else {
                return res.send('登录失败')
              }
            })
            .catch(err => {
              console.log(err);
            })
        }
      })
  },
    () => {
      console.log('链接失败');
    })
})
module.exports = router
